# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.StudentInfo.StudentImportAtt import StudentImportAtt
from LBT.GeneralOperation.StudentOperation import StudentOperation
from LBT.Resource.StudentInfo.StudentInfoAtt import StudentInfoAtt


class ImportStudent(lbtlogin, StudentImportAtt, StudentInfoAtt):
    '''导入学生信息模块
    1、检查页面信息。
    2、查询幼儿档案第一个班级，然后    新生+随机4位数字   。手机号为之前查询的学生
    另外准备这个班已有的学生及手机号。
    3、导入，成功1失败1，失败结果和刚才留存的匹配。成功结果去幼儿档案匹配。
    '''

    @lbtlogin.loginFun
    def test_importstudent(self):
        """导入
        :return:
        """
        self.docstr = '导入学生'
        dr = self.driver
        # 进入到学生档案模块，先查询一个已经存在的学生
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_studentinfo_url,
                                      self.student_topnavigation_class)
        stuopr = StudentOperation(dr)
        firststudent = dr.find_elements_by_xpath(self.studentinfo_students_xpath)[0]
        infodict = stuopr.getstudentinfo(firststudent)
        dr.switch_to.default_content()
        # 关闭所有之前的模块，重新打开
        self.moduleOp.close_modules()
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_import_url,
                                      self.student_topnavigation_class)
        # 先删除下载目录的所有文件，然后下载一个新的
        self.deleteAllFiles(self.defaultdownpath)
        dr.find_element_by_class_name(self.studentimport_download_class).click()
        errorstudentinfo = {}
        errorstudentname = "新生" + self.getRandomStr(4)
        errorstudentinfo['name'] = errorstudentname
        errorstudentinfo['class'] = str(infodict['class'])
        errorstudentinfo['sex'] = str(infodict['sex'])
        errorstudentinfo['phone'] = str(infodict['phone'])
        list = [infodict, errorstudentinfo]
        # 将两个学生信息写入进去
        file = self.defaultdownpath + self.studentimport_downloadtemplate_name
        stuopr.writeImportStudentExcel(file, list)
        dr.find_element_by_name(self.studentimport_upload_name).send_keys(file)
        self.webdriverwait(dr, dr.find_element_by_id(self.studentimport_uploadbutton_id))
        dr.find_element_by_id(self.studentimport_uploadbutton_id).click()
        # 等待按钮变灰
        self.webdriverwaitnotenable(dr, dr.find_element_by_id(self.studentimport_uploadbutton_id))
        resulttext = dr.find_element_by_class_name(self.studentimport_importresult_class).text
        # 校验成功和失败个数及失败人员姓名
        self.assertTrue(resulttext == self.studentimport_resluttext)
        errorstudentname1 = dr.find_element_by_xpath(self.studentimport_errorstudentname_xpath).text
        self.assertEqual(infodict['name'], errorstudentname1)
        # 校验导出文件里面的错误数据姓名和个数，符合要求
        self.deleteAllFiles(self.defaultdownpath)
        dr.find_element_by_id(self.studentimport_exporterrorbutton_id).click()
        file = self.defaultdownpath + self.studentimport_downloaderror_name
        errorlist = stuopr.readErrorStudentExcel(file)
        self.assertEqual(str(errorlist[1][0]), infodict['name'])
        self.assertTrue(errorlist.__len__()==2)
        #回到学生查询模块，查询学生是否存在
        dr.switch_to.default_content()
        self.moduleOp.close_modules()
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_studentinfo_url,
                                      self.student_topnavigation_class)
        dr.find_element_by_id(self.studentinfo_name_id).send_keys(errorstudentinfo['name'])
        dr.find_element_by_xpath(self.studentinfo_selectbutton_xpath).click()
        studentinfos=dr.find_elements_by_xpath(self.studentinfo_students_xpath)
        #查询学生个数和姓名是否匹配
        self.assertTrue(studentinfos.__len__()==1)
        selectstudent=stuopr.getstudentinfo(studentinfos[0])
        self.assertEqual(selectstudent["name"],errorstudentinfo['name'])